package models.usuario;

import play.*;
import play.data.validation.Email;
import play.data.validation.Required;
import play.db.jpa.*;
import play.libs.Codec;
import play.libs.Crypto;

import javax.persistence.*;

import models.Notificacao;
import models.livro.LivroLocatario;
import models.livro.LivroUsuario;

import controllers.Application;

import java.security.SecureRandom;
import java.security.Security;
import java.util.*;

@Entity
public class Usuario extends Model {
	
	@Email
	@Required
	public String email;
	@Required
	public String senha;
	public Boolean isAdmin;
	public String salt;
	@OneToOne
	public Perfil perfil;
	@OneToMany(mappedBy="proprietario")
	public List<LivroUsuario> livros = new ArrayList<LivroUsuario>();
	@OneToMany(mappedBy="locatario")
	public List<LivroLocatario> livrosEmprestados = new ArrayList<LivroLocatario>();
	@OneToMany(mappedBy="destinatario")
	public List<Notificacao> notificacoes = new ArrayList<Notificacao>();
	
	public Usuario(String login, String senha){
		SecureRandom rand = new SecureRandom();
		long num = rand.nextLong();
		this.isAdmin = false;
		this.email = login;
		this.salt = Codec.encodeBASE64(String.valueOf(num));
		this.senha = Application.encriptaSenha(senha, this.salt);
	}
	public Usuario(String login, String senha, Perfil perfil){
		SecureRandom rand = new SecureRandom();
		long num = rand.nextLong();
		this.isAdmin = false;
		this.email = login;
		this.salt = Codec.encodeBASE64(String.valueOf(num));
		this.senha = Application.encriptaSenha(senha, this.salt);
		this.perfil = perfil;
	}
}